import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * User: Thoughtworker
 * Date: 7/11/13
 * Time: 10:33 AM
 * To change this template use File | Settings | File Templates.
 */
public class UserDataBase {
    private HashMap<String, User> users;
    private UserInput input = new UserInput();
    private String whosLoggedName=null;

    public UserDataBase(ArrayList<User> userProfile) {
        users= new HashMap<String, User>();

        for (User my_user: userProfile){
            users.put(my_user.getUserName(),my_user);
        }

    }

    public boolean validateUser(String user) {
        return users.containsKey(user);
    }

    public boolean validatePassword(String userName, String password) {
            return password.equals(users.get(userName).getPassword());
    }


    public String getUserInputForUserName(){
        System.out.print("Username: ");
        String userName = input.getUserInput();
        if(this.validateUser(userName)){
            return userName;
        }
        else
            throw new IllegalArgumentException("User not found!");
    }

    public void getUserInputForPassword(String name){
        System.out.print("Password: ");
        String password = input.getUserInput();
        if (!(this.validatePassword(name, password))){
            throw new IllegalArgumentException("Invalid Password");
        }
    }


    public boolean checkUserCredentials() {
        try{
            String userName=getUserInputForUserName();
            getUserInputForPassword(userName);
            loginUser(userName);
            return true;
        }catch(IllegalArgumentException e)
        {
            System.out.println(e.getMessage());
            return false;
        }
    }

    public void loginUser(String nameOfUser){
        whosLoggedName = nameOfUser;
    }

    public boolean isSomeoneLoggedIn() {
        if (whosLoggedName==null) return false;
        else return true;
    }

    public void printLoggedInUser() {
        System.out.println("");
        System.out.println("Name: " + users.get(whosLoggedName).getName());
        System.out.println("Email: " + users.get(whosLoggedName).getEmail());
        System.out.println("Phone #: " + users.get(whosLoggedName).getPhone());
        System.out.println("");


    }

}
